package com.yhq.basic.thread;

import jdk.nashorn.internal.ir.IfNode;
import lombok.SneakyThrows;

import java.util.Random;

/**
 * @author hqyin
 * @date 2023/12/12 3:58 PM
 */
public class TestThreadCoreMemthod {
    public static void main(String[] args) {
        //测试yield
//        test_yield();
        //测试join
//        test_join();
        //测试interrupt
//        test_interrupt();
        //测试重复调用start方法
//        test_repeatStart();
        //测试优先级
//        test_priority();
    }

    private static void test_priority() {
        Runnable runnable = () -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+"执行完成");
        };

        int num = 9;
        Thread[] threads = new Thread[num];
        for (int i = 0; i < num; i++) {
            if(i<num/3) {
                threads[i] = new Thread(runnable,"高优先级线程-"+i);
                threads[i].setPriority(Thread.MAX_PRIORITY);
            } else if (i >= (num / 3) && i < (num / 3) * 2) {
                threads[i] = new Thread(runnable,"中优先级线程-"+i);
                threads[i].setPriority(Thread.NORM_PRIORITY);
            } else {
                threads[i] = new Thread(runnable,"低优先级线程-"+i);
                threads[i].setPriority(Thread.MIN_PRIORITY);
            }
        }

        for (Thread thread : threads) {
            thread.start();
        }
    }

    private static void test_repeatStart() {
        //测试多次调用start方法，会抛错，抛非法状态 if (threadStatus != 0) throw new IllegalThreadStateException();
        Thread thread1 = new Thread();
        thread1.start();
        thread1.start();
    }

    @SneakyThrows
    private static void test_interrupt() {
        Thread thread =  new Thread(new Thread2());
        thread.start();
        Thread.sleep(1);
        thread.interrupt();

        System.out.println("========================");

        Thread thread1 = new Thread(new Thread1());
        thread1.start();
        Thread.sleep(1);
        thread1.interrupt();

        Thread.yield();
    }

    private static void test_join() {
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName());
        },"ThreadA");

        Thread thread2 = new Thread(() -> {
            try {
                // thread2 让thread1执行完成后再执行
                thread1.join();
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName());
        },"ThreadB");

        Thread thread3 = new Thread(() -> {
            try {
                // thread3 让thread2执行完成后再执行
                thread2.join();
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName());
        },"ThreadC");

        thread1.start();
        thread2.start();
        thread3.start();

    }

    private static void test_yield() {
        Runnable runnable = () -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName()+"，i= "+i);
                Thread.yield();
            }
        };
        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        thread1.start();
        thread2.start();
    }

    public static class Thread2 implements Runnable {
        @Override
        public void run() {
            while (true) {
                System.out.println("Thread2线程常规状态："+Thread.currentThread().isInterrupted());
                if(Thread.currentThread().isInterrupted()){
                    System.out.println("Thread2线程Thread.currentThread().isInterrupted()状态："+Thread.currentThread().isInterrupted());
                    break;
                }
            }

        }
    }

    public static class Thread1 implements Runnable {
        @Override
        public void run() {
            while (true) {
                System.out.println("Thread1线程常规状态："+Thread.currentThread().isInterrupted());
                boolean flag = Thread.interrupted();
                if(flag){
                    System.out.println("Thread1线程Thread.interrupted()返回的状态："+flag+"，Thread1线程调用Thread.interrupted()方法之后线程状态"+Thread.currentThread().isInterrupted());
                    break;
                }
            }
        }
    }


}
